#include <ximera/system/application.hpp>
#include <ximera/logger.hpp>
#include <ximera/xml/xmldocument.hpp>
#include <ximera/xml/xmliterator.hpp>
#include <ximera/xml/xmlelement.hpp>

#include <iostream>

struct AppTestHandler : ximera::system::AppHandler
{
	virtual void onStart(ximera::system::AppEventArgs& args)
    {
        try
        {
            using namespace ximera;

            audioWorld.reset(args.app.audioSystem()->createWorld());
            listener.reset(audioWorld->createListener());
            sound.reset(audioWorld->loadSound(L"media/sounds/explosion.wav"));

            sound->loopMode(audio::LoopMode::On);
            sound->play();
        }
        catch (ximera::Exception& e)
        {
            args.app.shouldClose(true);
            XIMERA_LOG() << e << std::endl;
        }
    }

	virtual void onUpdate(ximera::system::UpdateEventArgs& args)
    {
        listener->position(listener->position() + ximera::math::Vector3::E1 * args.secs);
    }

private:
    ximera::SharedPtr<ximera::audio::IWorld> audioWorld;
    ximera::SharedPtr<ximera::audio::IListener> listener;
    ximera::SharedPtr<ximera::audio::ISound> sound;
};

int main()
{
    using namespace ximera;

    try
    {
        XIMERA_LOG().stream(&std::wcout);

        system::AppSettings settings;
        settings[L"ximera.plugins.audio"] = XIMERA_PLUGIN(L"XimeraOpenAL");

        system::Application app(settings);

        AppTestHandler handler;
        app.addHandler(handler);

        return app.run();
    }
    catch (ximera::Exception& e)
    {
        XIMERA_LOG() << e << std::endl;
    }
}
